<?php

namespace Kateba\GOOBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * Project controller.
 *
 */
class ProjectController extends Controller {

    /**
     * Finds and displays a Project entity.
     *
     */
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('KatebaGOOBundle:Project')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Project entity.');
        }

        return $this->render('KatebaGOOBundle:Project:show.html.twig', array(
                    'entity' => $entity,
                ));
    }

    public function showTypeAction($type, $page) {
        //the results per page number
        $itemsPerPage = 6;
        //the number of funding
        $count = 0;
        //the last page number used for pagination
        $lastPageNumber = 0;
        $projectRepo = $em = $this->getDoctrine()->getEntityManager()->getRepository('KatebaGOOBundle:Project');
        $results = $projectRepo->getProjects($type, $page, $itemsPerPage);
        $entities = $results['entities'];
        $count = $results['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }
        return $this->render('KatebaGOOBundle:Project:typeResults.html.twig', array(
                    'entities' => $entities,
                    'count' => $count,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber,
                    'type' => $type
                ));
    }

}
